package com.ebupt.migu.music.sms.service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ebupt.migu.music.common.entity.Page;
import com.ebupt.migu.music.common.exception.MyException;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.sms.mapper.SmsTmplMapper;

/**
 * @author liuzz
 * @date 2019/09/06
 */
@Service
public class SmsTmplService {

    @Autowired
    private SmsTmplMapper smsMapper;

    public Map<String, Object> addSmsTmpl(Map<String, Object> paramMap) {
        Map<String, Object> retMap = new HashMap<String, Object>();
        try {
            String content = paramMap.get("templateContent").toString();

            List<Map<String, Object>> smsTmplList = smsMapper.getSmsTmplById(paramMap);

            // 如果模板已存在，则返回错误
            if (smsTmplList.size() > 0) {
                retMap.put("code", StatusEnum.SMS_TEMPLATE_ERROR_SENSITIVE_WORD.getCode());
                retMap.put("msg", StatusEnum.SMS_TEMPLATE_ERROR_EXISTED.getMsg());
                return retMap;
            }
            smsMapper.addSmsTmpl(paramMap);
            Integer id = (Integer)paramMap.get("id");
            Map<String, Integer> dataMap = new HashMap<String, Integer>();
            dataMap.put("id", id);
            retMap.put("code", StatusEnum.SUCCESS.getCode());
            retMap.put("msg", "添加短信模板成功");
            retMap.put("data", dataMap);

        } catch (Exception e) {
            e.printStackTrace();

            throw new MyException(StatusEnum.ERROR, e.getMessage());

        }
        return retMap;
    }

    public Integer delSmsTmpl(Map<String, Object> paramMap) {
        try {
            return smsMapper.delSmsTmpl(paramMap);
        } catch (Exception e) {
            e.printStackTrace();

            throw new MyException(StatusEnum.ERROR, e.getMessage());
        }
    }

    public Page<Map<String, Object>> querySmsTmpl(Map<String, Object> paramMap) {
        Page<Map<String, Object>> page = new Page<Map<String, Object>>();
        try {
            Integer total = smsMapper.querySmsTmplCount(paramMap);
            List<Map<String, Object>> list = smsMapper.querySmsTmpl(paramMap);
            // 当前list中creator为英文登录名，需要查询出中文姓名
            for (Map<String, Object> temp : list) {
                String creator = temp.get("creator").toString();
                temp.put("creator", smsMapper.login_name_to_user_name(creator));
                temp.put("creatorId", creator);
            }
            page.setTotal(total);
            page.setData(list);
            return page;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(StatusEnum.ERROR, e.getMessage());
        }
    }

    public Page<List<Map<String, Object>>> queryCheckers(Map<String, Object> paramMap) {
        Page<List<Map<String, Object>>> page = new Page<List<Map<String, Object>>>();
        try {
            Integer total = smsMapper.queryCheckersCount(paramMap);
            List<Map<String, Object>> list = smsMapper.queryCheckers(paramMap);

            page.setTotal(total);
            page.setData(Collections.singletonList(list));
            return page;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(StatusEnum.ERROR, e.getMessage());
        }
    }

    public Map<String, Object> validateSensitiveWords(Map<String, Object> paramMap) {
        Map<String, Object> retMap = new HashMap<String, Object>();
        try {
            String content = paramMap.get("templateContent").toString();
            // 判断是否包含敏感词
            List<String> sensitiveWords = smsMapper.getSmsSensitiveWords(content);

            // 存在敏感词
            if (null != sensitiveWords && sensitiveWords.size() != 0) {
                retMap.put("code", StatusEnum.SMS_TEMPLATE_ERROR_SENSITIVE_WORD.getCode());
                retMap.put("msg", "存在敏感词" + sensitiveWords );
            } else {
                retMap.put("code", "0000");
                retMap.put("msg", "校验通过");
            }
        } catch (Exception e) {
            e.printStackTrace();

            throw new MyException(StatusEnum.ERROR, e.getMessage());

        }
        return retMap;
    }
}